package edu.fau.scanny.r;

import java.io.File;
import java.io.IOException;

import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.REngineException;
import org.rosuda.REngine.Rserve.RConnection;

public class RFactory {
	
	public static File plot(RConnection rc, String varToPlot, String ylab, String xlab) throws REngineException, REXPMismatchException, IOException{
		File plotFile = File.createTempFile("plot_", ".png");
		String plotFileS = plotFile.getAbsolutePath().replace("\\", "/");
		RUtils.runRExp(rc, RUtils.png(plotFileS));
		RUtils.runRExp(rc, RUtils.plot(varToPlot, ylab, xlab));
		RUtils.devOff(rc);
		return plotFile;
	}
	
	
//	 ratio<-x[,1]/x[,2];	
//	 orderedratio<<-sort.int(ratio);
//	 orderedratio<<-orderedratio[orderedratio<2];
	public static void rankOrderedRatio(RConnection rc, String tableVar, String orderedRatio) throws REngineException, REXPMismatchException{
		String ratio = String.format("ratio<-%s[,1]/%s[,2];", tableVar,tableVar);
		RUtils.runRExp(rc, ratio);
		String orderedRatioFxn = String.format("%s<<-sort.int(ratio);",orderedRatio);
		RUtils.runRExp(rc, orderedRatioFxn);
		String limitRatio = String.format("%s<-%s[%s<2];",orderedRatio,orderedRatio,orderedRatio);
		RUtils.runRExp(rc, limitRatio);
	}
	

}
